package com.avery.mile.resource;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Properties;

/**
 * Created by avery on 2018-12-27.
 */
public class TestResource {
    public static void main(String[] args) {
        new TestResource().print("core-site.properties");
        new TestResource().print("conf/first/biz.properties");
        new TestResource().print("conf/second/biz.properties");
        new TestResource().print("conf/invoke/biz.properties");
    }

    public void print(String propFileName) {
        System.out.println("|==== " + propFileName);
        final Properties properties = new Properties();
        InputStream inputStream = com.avery.mile.resource.first.TestResource.class.getClassLoader().getResourceAsStream(propFileName);
        try {
            properties.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        properties.list(System.out);
    }


    private static void listAllFile(String s, File file) {
        if (file.isDirectory()) {
            System.out.println(s + ">" + file.getName());
            for (File listFile : file.listFiles()) {
                listAllFile(s + "--", listFile);
            }

        } else {
            System.out.println(s + file.getName());
        }
    }



    public void printTree() {


        final URL resource = TestResource.class.getClassLoader().getResource(".");
        System.out.println(resource);
        try {
            final File file = new File(resource.toURI());
            final File parentFile = file.getParentFile();
            System.out.println(parentFile);
            listAllFile("|-", file);
        } catch (
                  URISyntaxException e) {
            e.printStackTrace();
        }

    }


}
